/*
 *  Copyright (C) 2005-2007 Renier Roth
 *  www.renier.de
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.renier.jkeepass.components;

import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.dnd.Autoscroll;
import java.awt.dnd.DnDConstants;

import javax.swing.JTree;
import javax.swing.tree.TreeSelectionModel;
 
/**
 * ContainerElementTree
 *
 * @author <a href="mailto:software@renier.de">Renier Roth</a>
 */
public class ContainerElementTree extends JTree implements Autoscroll {
 
  private static final long serialVersionUID = -7988033097980561505L;
  private Insets autoscrollInsets = new Insets(20, 20, 20, 20); // insets
 
  public ContainerElementTree() {
    setAutoscrolls(true);
    setRootVisible(true); 
    setShowsRootHandles(false);//to show the root icon
    getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); //set single selection for the Tree
    setEditable(false);
    new ContainerElementTreeTransferHandler(this, DnDConstants.ACTION_COPY_OR_MOVE);
  }
 
  /*
   * (non-Javadoc)
   * @see java.awt.dnd.Autoscroll#autoscroll(java.awt.Point)
   */
  public void autoscroll(Point cursorLocation)  {
    Insets insets = getAutoscrollInsets();
    Rectangle outer = getVisibleRect();
    Rectangle inner = new Rectangle(outer.x+insets.left, outer.y+insets.top, outer.width-(insets.left+insets.right), outer.height-(insets.top+insets.bottom));
    if (!inner.contains(cursorLocation))  {
      Rectangle scrollRect = new Rectangle(cursorLocation.x-insets.left, cursorLocation.y-insets.top, insets.left+insets.right, insets.top+insets.bottom);
      scrollRectToVisible(scrollRect);
    }
  }
 
  /*
   * (non-Javadoc)
   * @see java.awt.dnd.Autoscroll#getAutoscrollInsets()
   */
  public Insets getAutoscrollInsets()  {
    return (autoscrollInsets);
  }
 
}
